#include "tProduction.h"

int nbPaletteParLot, nbCartonParPalette, nbPieceParCarton;
int arret = 0;
int errnumber = 0;

void tProduction()
{
	Trame * trame;
	Trame * trameLog;
	MsgOrdreCond * msgOrdreCond;
	MsgOrdreExpe * msgOrdreExpe;
	int quantity1, quantity2, numberLot;
	int numCommande = 0;
	int numQuai = 0;
	int bytesToWrite = 0;
	char result;
    char currentDate[TAILLE_DATE_FORMAT];
    
	for( ; ; ){
		trame = (Trame *) malloc(sizeof(Trame));
		
		if((msgQReceive(balMsgRecv, (char *) trame, TAILLEMSG_BALMESSAGELOG, WAIT_FOREVER)) != ERROR)
		{
			DBG("Fin de Lecture de Message Recu : ---%s--- ", trame->content);			
			if(trame->content[0] == 'C')
			{
				numberLot = char5ToInt(trame->content,22);
				quantity1 = char5ToInt(trame->content,28);
				quantity2 = char5ToInt(trame->content,34);
				if(quantity1 > 0)
				{
					msgOrdreCond = (MsgOrdreCond *) malloc(sizeof(MsgOrdreCond));
					msgOrdreCond->type = PIECE1;
					msgOrdreCond->quantite = quantity1;
					msgOrdreCond->num = numberLot;
					msgQSend(balOrdreCond, (char *) msgOrdreCond, sizeof(MsgOrdreCond), WAIT_FOREVER, MSG_PRI_NORMAL);
					free(msgOrdreCond);
				}
				if(quantity2 > 0)
				{
					msgOrdreCond = (MsgOrdreCond *) malloc(sizeof(MsgOrdreCond));
					msgOrdreCond->type = PIECE2;
					msgOrdreCond->quantite = quantity2;
					msgOrdreCond->num = numberLot;
					msgQSend(balOrdreCond, (char *) msgOrdreCond, sizeof(MsgOrdreCond), WAIT_FOREVER, MSG_PRI_NORMAL);
					free(msgOrdreCond);
				}
			}
			else if(trame->content[0] == 'X')
			{
				quantity1 = char5ToInt(trame->content,22);
				quantity2 = char5ToInt(trame->content,28);
				if((quantity1 > 0) || (quantity2 > 0))
				{
					msgOrdreExpe = (MsgOrdreExpe *) malloc(sizeof(MsgOrdreExpe));
					msgOrdreExpe->quantite1 = quantity1;
					msgOrdreExpe->quantite2 = quantity2;
					msgOrdreExpe->numCommande = numCommande++;  
					msgOrdreExpe->quai = numQuai++;        
					msgQSend(balOrdreExpe, (char *) msgOrdreExpe, sizeof(MsgOrdreExpe), WAIT_FOREVER, MSG_PRI_NORMAL);
					free(msgOrdreExpe);
				}
			}
			else if(trame->content[0] == 'I')
			{
				//nbPaletteParLot = char5ToInt(trame->content,22);
				nbCartonParPalette = char5ToInt(trame->content,22);
				nbPieceParCarton = char5ToInt(trame->content,28);
				
				// envoyer message a log
			    date(currentDate);
				trameLog = (Trame *) malloc(sizeof(Trame));    
			    bytesToWrite = sprintf(trameLog->content, "[%s] Init: %d pieces par carton, %d cartons par palette\r\n", 
			    		currentDate, nbPieceParCarton, nbCartonParPalette, nbPaletteParLot); 
			    trameLog->lenght = bytesToWrite;
			    msgQSend(balMessageLog, (char *) trameLog, sizeof(Trame), WAIT_FOREVER, MSG_PRI_NORMAL);
			    
			    // Liberation de la memoire
			    free(trameLog);
			}
			else if(trame->content[0] == 'R')
			{
				result = trame->content[21];
				errnumber = char5ToInt(trame->content,22);
				if(result == 'A') arreter();
				if(result == 'C') continuer();
				if(result == 'N') nouveauCarton();
			}		
			else if(trame->content[0] == 'A')
			{
				arretProduction();
			}		
			else 
			{
				DBG("Information inconnu\n");
			}
		}
		else{
			DBG("Erreur sur Lecture de Message Recu\n");	
		}
		free(trame);
		taskDelay(6);
		/*if(arret) 
		{
			DBG("tProduction est termine");
			return;
		}*/
	}
}

int char5ToInt(char * c, int startNumber)
{
	char number[1];
	int i = 0;
	int r = 0;
	for (i = 0;i<5;i++)
	{
		number[0] = c[startNumber+i];
		//printf( "%d : %c\n", i, number[i]);
		r += atoi(number)*(pow(10,4-i));
		//printf( "%d \n", r);
	}
	return r;	
}

void intToChar5(int n, char * c)
{
	char number[5];
	int i = 0;
	int j = 0;
	int len = 0;
	int length0 = 5-len;
	sprintf(number,"%d",n);
	len = strlen(number);
	length0 = 5-len;
	c[0] = '\0';
	//printf("number : %s length %d, length0 : %d\n", number, len, length0);
	for (i = 0; i<length0; i++)
	{
		c[i] = '0';
	}
	for (j = 0; j<len; j++)
	{
		//printf( "%d : %s\n", j, c);
		c[j+length0] = number[j];
		//printf( "%d : %s\n", j, c);
	}
	c[5] = '\0';
	i =0;
	j =0; 
	return;	
}

void testConvertInt()
{
	int i = 1258;
	char buffer[6];

	intToChar5(i,buffer);
	printf( "%s\n", buffer);
	
	i = char5ToInt(buffer,0);
	
	printf( "%d\n", i);
}

void arreter()
{
	int i = 0;
	int nbOrder = 0;
	MsgOrdreCond * msgOrdreCond;
	//clear All Order
	nbOrder = msgQNumMsgs(balOrdreCond);
	
	
	for(i = 0;i<nbOrder;i++)
	{
		msgOrdreCond = (MsgOrdreCond *) malloc(sizeof(MsgOrdreCond));
		msgQReceive(balOrdreCond, (char *) msgOrdreCond, sizeof(MsgOrdreCond), WAIT_FOREVER);
		free(msgOrdreCond);
	}

	taskDelete(carteTache[T_CONDITIONNEMENT]);
	carteTache[T_CONDITIONNEMENT] = taskSpawn("conditionnement", 100, 0, 100,  (FUNCPTR) tConditionnement, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
	taskSuspend(carteTache[T_CONDITIONNEMENT]);
}

void continuer()
{
	nouvelleCarton = 0;
	reprise(errnumber);
}

void nouveauCarton()
{
	nouvelleCarton = 1;
	reprise(errnumber);
}

void arretProduction()
{
	MsgOrdreCond * msgOrdreCond;	
	msgOrdreCond = (MsgOrdreCond *) malloc(sizeof(MsgOrdreCond));
	msgOrdreCond->type = MSG_FIN;
	msgOrdreCond->quantite = 0;
	msgOrdreCond->num = 0;
	msgQSend(balOrdreCond, (char *) msgOrdreCond, sizeof(MsgOrdreCond), WAIT_FOREVER, MSG_PRI_NORMAL);
	free(msgOrdreCond);
	arret = 1;
}
